package org.thanos.demo.producer;

import com.lmax.disruptor.RingBuffer;
import org.thanos.demo.entity.LogEvent;

public class LogEventProducer {

    private final RingBuffer<LogEvent<String>> ringBuffer;

    public LogEventProducer(RingBuffer<LogEvent<String>> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    public void pub(String topic, String content) {
        //ringBuffer类似一个队列，next就是下一个槽
        long seq = ringBuffer.next();
        try {
            //用seq索引取出一个空到事件用于填充
            LogEvent<String> logEvent = ringBuffer.get(seq);
            logEvent.setTopic(topic);
            logEvent.setContent(content);
        } finally {
            //最终发布事件，很重要
            ringBuffer.publish(seq);
        }
    }
}
